import { Body, Controller, Post, Req, UseInterceptors } from '@nestjs/common';
import {
    ApiBearerAuth,
    ApiOkResponse,
    ApiOperation,
    ApiTags,
    ApiExtraModels,
    ApiExcludeEndpoint,
} from '@nestjs/swagger';


import { UserService } from './user.service';

import { CreateUserDto } from './dto/create-user.dto';
import { CreateTokenDto } from './dto/create-token.dto';
import { ResultData } from '@/common/utils/result';
import { User } from '@/modules/system/user/entities/user.entity';
import { NotToken } from '@/decorators/not-token.decorator';
import { LoginUser } from '@/modules/system/user/dto/login-user.dto';
import { LogInterceptor } from '@/interceptors/log.inerceptor';

@ApiTags('登录注册')
@ApiExtraModels(ResultData, User, CreateTokenDto)
@ApiBearerAuth()
@Controller('system/user')
@UseInterceptors(LogInterceptor)
export class BaseController {
    constructor(private readonly userService: UserService) {
    }
    
    // @Post("register")
    // @ApiOperation({ summary: "用户注册" })
    // @AllowAnon()
    // async create(@Body() user: CreateUserDto): Promise<ResultData> {
    // 	return await this.userService.create(user);
    // }
    
    @Post('/login')
    @NotToken()
    @ApiOperation({
        summary: '登录',
        security: [], // 关键配置：空数组表示该接口不需要任何认证
    })
    async login(@Body() dto: LoginUser): Promise<ResultData> {
        return await this.userService.login(dto.username, dto.password);
    }
    
    @Post('update/token')
    @ApiOperation({ summary: '刷新token' })
    async updateToken(@Req() req): Promise<ResultData> {
        return await this.userService.updateToken(req.user.id);
    }
}
